package org.dromara.fms.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.dromara.common.web.core.BaseService;
import org.springframework.stereotype.Service;
import org.dromara.fms.domain.bo.FmsBillBo;
import org.dromara.fms.domain.vo.FmsBillVo;
import org.dromara.fms.domain.FmsBill;
import org.dromara.fms.mapper.FmsBillMapper;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 财务单据Service业务层处理
 *
 * @author wenrui Su
 * @date 2024-03-22
 */
@RequiredArgsConstructor
@Service
public class FmsBillServiceImpl extends BaseService {

    private final FmsBillMapper baseMapper;

    /**
     * 查询财务单据
     */
    public FmsBillVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询财务单据列表
     */
    public TableDataInfo<FmsBillVo> queryPageList(FmsBillBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<FmsBill> lqw = buildQueryWrapper(bo);
        Page<FmsBillVo> result = baseMapper.selectPageBillList(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询财务单据列表
     */
    public List<FmsBillVo> queryList(FmsBillBo bo) {
        LambdaQueryWrapper<FmsBill> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<FmsBill> buildQueryWrapper(FmsBillBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<FmsBill> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getTypeId() != null, FmsBill::getTypeId, bo.getTypeId());
        lqw.like(StringUtils.isNotBlank(bo.getName()), FmsBill::getName, bo.getName());
        lqw.eq(bo.getPartnerId() != null, FmsBill::getPartnerId, bo.getPartnerId());
        lqw.between(params.get("beginPlanTime") != null && params.get("endPlanTime") != null,
            FmsBill::getPlanTime ,params.get("beginPlanTime"), params.get("endPlanTime"));
        lqw.between(params.get("beginExecutionTime") != null && params.get("endExecutionTime") != null,
            FmsBill::getExecutionTime ,params.get("beginExecutionTime"), params.get("endExecutionTime"));
        return lqw;
    }

    /**
     * 新增财务单据
     */
    public Boolean insertByBo(FmsBillBo bo) {
        FmsBill add = MapstructUtils.convert(bo, FmsBill.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改财务单据
     */
    public Boolean updateByBo(FmsBillBo bo) {
        FmsBill update = MapstructUtils.convert(bo, FmsBill.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(FmsBill entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除财务单据
     */
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
